package com.pw.study.flink.transform;

import com.pw.study.flink.entities.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

public class TransFormAgg {
    public static void main(String[] args) {
        //dealAgg();
        dealMin();

    }

    private static void dealMinKey() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        List<WaterSensor> s = Arrays.asList(new WaterSensor("101", 100L, 33),
                new WaterSensor("22", 10L, 3),
                new WaterSensor("101", 55L, 100),
                new WaterSensor("22", 20L, 66));
        KeyedStream<WaterSensor, String> stream = env.fromCollection(s).keyBy(waterSensor -> waterSensor.getId());
        stream.min("vc").print("minKey");

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dealMin() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        List<WaterSensor> s = Arrays.asList(new WaterSensor("101", 100L, 33),
                new WaterSensor("22", 10L, 3),
                new WaterSensor("101", 55L, 100),
                new WaterSensor("22", 20L, 66));
        KeyedStream<WaterSensor, String> stream = env.fromCollection(s).keyBy(waterSensor -> waterSensor.getId());
        stream.minBy("vc").print("minBy");
        stream.minBy("vc",false).print("f");

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dealAgg() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        KeyedStream<Integer, String> stream = env.fromElements(1, 2, 3, 4, 5, 6).keyBy(x -> x % 2 == 0 ? "偶数" : "奇数");
        stream.sum(0).print("sum");
        stream.min(0).print("min");
        stream.max(0).print("max");


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
